package cn.autumnorange.app.user.provider.repository.impl;

import cn.autumnorange.app.common.repository.impl.BaseCodeRepositoryImpl;
import cn.autumnorange.app.common.util.StringUtils;
import cn.autumnorange.app.user.provider.entity.SysUserEntity;
import cn.autumnorange.app.user.provider.repository.SysUserRepository;
import org.hibernate.transform.Transformers;
import org.hibernate.type.BooleanType;
import org.hibernate.type.LongType;
import org.hibernate.type.StringType;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SysUserRepositoryImpl extends BaseCodeRepositoryImpl<SysUserEntity>
    implements SysUserRepository {
  @Override
  public SysUserEntity getUserByIdCodeEmailMobile(String getUserByWhitchType, String var) {
    if (StringUtils.isBlank(var)) {
      return null;
    }
    String sql =
        "SELECT u.* FROM sys_user u WHERE u."
            + getUserByWhitchType
            + " =:"
            + getUserByWhitchType
            + " AND u.isDelete =0";
    Map<String, Object> conditionParams = new HashMap<>(1);
    Map<String, Type> returnType = new HashMap<>(1);
    returnType.put("id", new LongType());
    returnType.put("code", new StringType());
    returnType.put("password", new StringType());
    returnType.put("accountNonExpired", new BooleanType());
    returnType.put("accountNonLocked", new BooleanType());
    returnType.put("credentialsNonExpired", new BooleanType());
    returnType.put("enabled", new BooleanType());

    conditionParams.put(getUserByWhitchType, var);
    List<SysUserEntity> userList =
        findByNativeQuery(
            sql, conditionParams, Transformers.aliasToBean(SysUserEntity.class), returnType);

    if (userList.size() > 1) {
      throw new RuntimeException("login user has " + (userList.size()));
    }
    if (userList.size() != 0) {
      return userList.get(0);
    }
    return null;
  }

  public SysUserEntity findSysUserByCode(String code) {
    findByFiled("code", code);
    return null;
  }

  //    @Override
  //    public SysUserEntity setLoginUserRole(SysUserEntity user) {
  //        // 查询用户权限,多个权限能同级但不能上下级
  //        Map<String, Object> paramMap = new HashMap();
  //        paramMap.put("userId", user.getId());
  //        String sql = "SELECT ru.* from sys_role_user ru where ru.userId=:userId";
  //        List<SysRoleUserEntity> resRoleUser =
  //                findByNativeQuery(sql, paramMap,
  // Transformers.aliasToBean(SysRoleUserEntity.class));
  //        if (resRoleUser != null && resRoleUser.size() > 0) {
  //            paramMap.clear();
  //            List<String> paramList = new ArrayList();
  //            for (SysRoleUserEntity roleUser : resRoleUser) {
  //                paramList.add(roleUser.getRoleId());
  //            }
  //            paramMap.put("roleIdList", paramList);
  //            //      sql = "SELECT r.id,r.name from sys_role r where r.id in (:roleIdList)";
  //            List<SysRoleEntity> resRoleList = sysRoleRepository.findRolesByRoleIdList(paramMap);
  //            //      查询该用户所有权限所有菜单
  //            if (resRoleList != null && resRoleList.size() > 0) {
  //                user.setRoles(resRoleList);
  //                sql = "SELECT * from sys_role_menu where roleId in(:roleIdList)";
  //                //                用户多个权限下权限不为上下级关系只能同级,或不同级等级差，
  //                // 只需要拿到最高级别权限的权限菜单中间表记录，如果最高级别权限有多个，则需要同时都获取到
  //                List<SysRoleMenuEntity> roleMenuList =
  //                        findByNativeQuery(sql, paramMap,
  // Transformers.aliasToBean(SysRoleMenuEntity.class));
  //                if (roleMenuList != null && roleMenuList.size() > 0) {
  //                    paramMap.clear();
  //                    paramList.clear();
  //                    for (SysRoleMenuEntity roleMenu : roleMenuList) {
  //                        paramList.add(roleMenu.getMenuId());
  //                    }
  //                    paramMap.put("menuIdList", paramList);
  //                    //          sql = "SELECT * from sys_menu where id in(:menuIdList)";
  //                    //          List<SysMenuEntity> resMenuList =
  //                    //              findByNativeQuery(sql, paramMap,
  //                    // Transformers.aliasToBean(SysMenuEntity.class));
  //                    List<SysMenuEntity> resMenuList =
  // sysMenuRepository.findMenusByMenuIdList(paramMap);
  //                    if (resMenuList != null && resMenuList.size() > 0) {
  //                        //            将菜单menu归类到对应用户权限下
  //                        resMenuList:
  //                        for (SysMenuEntity menuEntity : resMenuList) {
  //                            for (SysRoleMenuEntity sysRoleMenuEntity : roleMenuList) {
  //                                if (menuEntity.getId().equals(sysRoleMenuEntity.getMenuId())) {
  //                                    for (SysRoleEntity roleEntity : resRoleList) {
  //                                        if
  // (roleEntity.getId().equals(sysRoleMenuEntity.getRoleId())) {
  //                                            TreeSet<SysMenuEntity> menuSet =
  // roleEntity.getMenus();
  //                                            if (menuSet == null) {
  //                                                roleEntity.setMenus(null);
  //                                                menuSet = roleEntity.getMenus();
  //                                            }
  //                                            menuSet.add(menuEntity);
  //                                            continue resMenuList;
  //                                        }
  //                                    }
  //                                }
  //                            }
  //                        }
  //                        // 将菜单menu累计上下级
  //
  //                        //            登陆菜单
  //                        UpDownTreeEntityService<SysMenuEntity> upDownTreeEntityService =
  //
  // UpDownTreeEntityServiceImpl.createUpDownTreeEntityService(SysMenuEntity.class);
  //                        user.setMenus(upDownTreeEntityService.upDownTreeEntity(resMenuList));
  //                    }
  //                    //    需要更新登录成功后的参数信息无法传request获取ip，
  //                    // 在sysUserRepository.update()无法获取当前线程的登陆者对象SysUserEntity u =
  //                    // SysUserUtils.getCurrentSysUser();还没set进去，
  //                    // system.currentTimeMillis()));
  //                    //    在AuthenticationSuccessHandlerImpl登录成功处理类可以解决这些问题
  //                    //    sysUserRepository.update(user);
  //                }
  //            }
  //        }
  //        return user;
  //    }
}
